package com.lingdong.onlinejudge.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.lingdong.onlinejudge.annotation.AuthCheck;
import com.lingdong.onlinejudge.common.BaseResponse;
import com.lingdong.onlinejudge.common.ErrorCode;
import com.lingdong.onlinejudge.common.ResultUtils;
import com.lingdong.onlinejudge.exception.BusinessException;
import com.lingdong.onlinejudge.model.dto.match.MatchCreateDto;
import com.lingdong.onlinejudge.model.dto.match.MatchQueryDto;
import com.lingdong.onlinejudge.model.dto.match.MatchUpdateDto;
import com.lingdong.onlinejudge.model.entity.Match;
import com.lingdong.onlinejudge.model.entity.Topic;
import com.lingdong.onlinejudge.model.enums.UserRoleEnum;
import com.lingdong.onlinejudge.model.vo.match.MatchVo;
import com.lingdong.onlinejudge.service.MatchService;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

/**
 * 比赛
 * @author lzw
 */

@RestController
@RequestMapping("/match")
public class MatchController {

    @Resource
    private MatchService matchService;

    /**
     * 创建比赛
     * @param matchCreateDto 比赛创建信息
     * @param token token
     * @return 返回值
     */
    @AuthCheck(mustRole = "admin")
    @PostMapping("/create")
    public BaseResponse<Boolean> createMatch(@RequestBody MatchCreateDto matchCreateDto,
                                             @RequestHeader("token") String token) {
        if (matchCreateDto == null){
            throw new BusinessException(ErrorCode.PARAMS_ERROR);
        }
        Boolean response = matchService.createMatch(matchCreateDto,token);
        return ResultUtils.success(response);
    }

    /**
     * 更新比赛
     * @param matchUpdateDto 比赛更新信息
     * @return 返回值
     */
    @AuthCheck(mustRole = "admin")

    @PutMapping("/update")
    public BaseResponse<Boolean> updateMatch(@RequestBody MatchUpdateDto matchUpdateDto) {
        if (matchUpdateDto == null){
            throw new BusinessException(ErrorCode.PARAMS_ERROR);
        }
        Boolean response = matchService.updateMatch(matchUpdateDto);
        return ResultUtils.success(response);
    }

    /**
     * 查询比赛列表
     * @param matchName 比赛名称
     * @return 返回值
     */
    @GetMapping("/list")
    public BaseResponse<List<Match>> getMatchList(@RequestParam String matchName) {
        List<Match> response = matchService.getMatchList(matchName);
        return ResultUtils.success(response);
    }

    /**
     * 查询比赛详情
     * @param id 比赛id
     * @return 返回值
     */
    @GetMapping("/query/{id}")
    public BaseResponse<MatchVo> getMatch(@PathVariable Long id,@RequestHeader("token") String token) {
        if (id == null){
            throw new BusinessException(ErrorCode.PARAMS_ERROR);
        }

        MatchVo response = matchService.getMatch(id,token);
        return ResultUtils.success(response);
    }

    /**
     * 分页查询比赛列表
     * @param matchQueryDto 比赛查询信息
     * @return 返回值
     */
    @PostMapping("/query/page")
    public BaseResponse<Page<Match>> queryMatchByPage(@RequestBody MatchQueryDto matchQueryDto) {
        if (matchQueryDto == null){
            throw new BusinessException(ErrorCode.PARAMS_ERROR);
        }
        Page<Match> response = matchService.queryMatchByPage(matchQueryDto);
        return ResultUtils.success(response);
    }

    /**
     * 删除比赛
     * @param id 比赛id
     * @return 结果
     */
    @AuthCheck(mustRole = "admin")

    @DeleteMapping("/delete")
    public BaseResponse<Boolean> deleteMatch(@RequestParam Long id) {
        if (id == null){
            throw new BusinessException(ErrorCode.PARAMS_ERROR);
        }
        Boolean response = matchService.removeById(id);
        return ResultUtils.success(response);
    }



    /**
     * 判断比赛是否结算
     * @param matchId 比赛id
     */
    @GetMapping("/isMatchFinish")
    public BaseResponse<Boolean> isMatchFinish(@RequestParam Long matchId){
        return ResultUtils.success(matchService.isMatchFinish(matchId));
    }

    /**
     * 根据比赛id获取试题列表
     * @param matchId 比赛id
     */
    @GetMapping("/topicList")
    public BaseResponse<List<Topic>> getTopicByMatchId(Long matchId){
        return ResultUtils.success(matchService.getTopicByMatchId(matchId));
    }



}
